Ext.namespace("TR.admin.security");

TR.admin.security.user = function(panelId, title, iconClz){

	var userStore = new Ext.data.JsonStore({
		url : "admin/user/findUserByCon",
		totalProperty : "total",
		root : "rows",
		autoLoad : true,
		fields : ["id", "createDate", "updateDate", "accountExpired", "accountLocked",
			"credentialsExpired", "status", "des", "realName", "username"
		],
		listeners : {
			beforeload : function(s){
				s.removeAll();
				Ext.apply(s.baseParams, {
					status : statusCombo.getValue(),
					con : queryField.getValue()
				});
			}
		}
	});
	var userSm = new Ext.grid.RowSelectionModel({
		singleSelect : true
	});
	var userCm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
		header : "用户名",
		dataIndex : "username"
	}, {
		header : "姓名",
		dataIndex : "realName"
	}, {
		header : "状态",
		dataIndex : "status",
		renderer : renderStatus
	}, {
		header : "创建时间",
		dataIndex : "createDate",
		renderer : renderDate,
		width : 120
	}, {
		header : "更新时间",
		dataIndex : "updateDate",
		renderer : renderDate,
		width : 120
	}, {
		header : "描述",
		dataIndex : "des",
		width : 300
	}]);
	var resetPwdBtn = {
		text : "重置密码",
		iconCls : "reset",
		handler : resetPwd
	};
	var optBar = genOptBarByFn(create, null, update, toggle, importIn);

	optBar.push(resetPwdBtn);
	var statusCombo = genQueryStatusCombo();
	var queryField = new Ext.form.TextField({
		width : 100
	});
	var queryBar = ["状态:", statusCombo, "关键字:", queryField, {
		text : "查询",
		iconCls : "query",
		handler : function(){
			userStore.reload();
		}
	}];
	var userGrid = new Ext.grid.GridPanel({
		store : userStore,
		sm : userSm,
		cm : userCm,
		tbar : genToolBar(optBar, queryBar, panelId),
		bbar : genPageBar(userStore)
	});
	return new Ext.Panel({
		id : panelId,
		title : title,
		iconCls : iconClz,
		closable : true,
		layout : "fit",
		layoutConfig : {
			align : "stretch"
		},
		items : [userGrid]
	});

	function genAddOrUpdateWin(url){
		return new Ext.Window({
			title : "新增或更新用户",
			layout : "fit",
			modal : true,
			iconCls : "save",
			width : 300,
			height : 250,
			plain : true,
			bodyStyle : 'padding:5px;',
			items : genForm(url)
		});
		function genForm(url){
			return new Ext.form.FormPanel({
				url : url,
				labelWidth : 50,
				labelAlign : "right",
				baseCls: 'x-plain',
				defaults : {
					xtype : "textfield",
					anchor : "95%"
				},
				items : [{
					xtype : "hidden",
					name : "id",
					value : "-1"
				}, {
					fieldLabel:"用户名",
					name : "username"
				}, {
					fieldLabel : "姓名",
					name : "realName"
				}, genStatusCombo(), {
					fieldLabel : "描述",
					name : "des",
					xtype : "textarea"
				}],
				buttonAlign : "center",
				buttons : [{
					text : "保存",
					iconCls : "save",
					handler : function(b){
						submitForm(b.findParentByType("form"), refreshGrid);
						function refreshGrid(){
							userStore.reload();
						}
					}
				}, {
					text : "重置",
					iconCls : "reset",
					type : "reset",
					handler : function(b){
						b.findParentByType("form").getForm().reset();
					}
				}]
			});
		}
	}
	function create(){
		var win = genAddOrUpdateWin("admin/user/add");
		win.show();
	}
	function del(){
		var rec =userSm.getSelected();
		if(rec==null){
			showFailMsg("请选择用户");
			return;
		}
		ajaxRequest({
			url : "admin/user/delete/" + rec.get("id"),
			callback : function(){
				userStore.reload();
			}
		});
	}
	function update(){
		var rec =userSm.getSelected();
		if(rec==null){
			showFailMsg("请选择用户");
			return;
		}
		var win = genAddOrUpdateWin("admin/user/update");
		win.findByType("form")[0].getForm().loadRecord(rec);
		win.findByType("form")[0].get(1).setReadOnly(true);
		win.show();
	}
	function toggle(){
		var rec =userSm.getSelected();
		if(rec==null){
			showFailMsg("请选择用户");
			return;
		}
		ajaxRequest({
			url : "admin/user/toggleStatus/" + rec.get("id"),
			callback : function(){
				userStore.reload();
			}
		});
	}
	function importIn(){
		var form = new Ext.form.FormPanel({
			fileUpload : true,
			frame : true,
			labelWidth: 30,
			url : "admin/user/importIn",
			defaults : {
				anchor : "100%"
			},
			items : [{
				fieldLabel : "文件",
				emptyText: '请选择文件',
				anchor: '100%',
				allowBlank: false,
				xtype : "fileuploadfield",
				name : 'file',
				buttonText : "浏览"
			}],
			buttons : [{
				text : "确定",
				iconCls : "save",
				handler : function(b){
					submitForm(b.findParentByType("form"), refreshGrid);
					function refreshGrid(){
					}
				}
			}, {
				text : "重置",
				iconCls : "reset",
				handler : function(b){
					b.findParentByType("form").getForm().reset();
				}
			}]
		});
		var win = new Ext.Window({
			title : "上传文件",
			width : 300,
			height : 100,
			layout : "fit",
			frame : true,
			items : [form]
		});
		win.show();
	}
	function resetPwd(){
		var rec =userSm.getSelected();
		if(rec==null){
			showFailMsg("请选择用户");
			return;
		}
		ajaxRequest({
			url : "admin/user/resetPwd/" + rec.get("id")
		});
	}
}

registerModule("TR.admin.security.user", TR.admin.security.user);